﻿/**
 * 加载数据
 */
$(function() {
	getSource();
});
function getSource(){
	$("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,
		cssStyle:'grayr',
		limit : 10,
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "terminalList",
		  dataType: 'json',
		  //param:"limit="+$("#limit").val(),
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	}); 
}
/**
 * 绑定数据
 * @param data
 */
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html = "";
	if(data.total > 0){
		$.each(data.source,function(i,terminal){
			html += '<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)">';// ondblclick="editAjax('+terminal.id+')"
			html +='<td style="padding-left: 5px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+terminal.id+'"/></td>';
			html += '<td>'+(terminal.fname == undefined ? "" : terminal.fname)+'</td>';
			if(terminal.fdevicetype == 0){
				html += '<td>电脑</td>';
			}else if(terminal.fdevicetype == 1){
				html += '<td>机顶盒</td>';
			}else if(terminal.fdevicetype == 2){
				html += '<td>手机</td>';
			}else if(terminal.fdevicetype == 3){
				html += '<td>Pad</td>';
			}
			html += "<td>"+((terminal.fdevicetype != 1) ? "" : (terminal.ftype == undefined)? "" :terminal.ftype == 'HD' ? '高清':'标清 ') +"</td>";
			html += "<td>"+((terminal.fdevicetype != 1) ? "" : (terminal.ffont == undefined)? "" :terminal.ffont==0 ?'点阵['+terminal.ffontsize+'px]':'矢量')+"</td>";
			html += "<td>"+((terminal.fdevicetype != 1) ? "" : (terminal.fresolutionx == "" ||terminal.fresolutionx == undefined)? "": terminal.fresolutionx+'×'+terminal.fresolutiony)+'</td>';
			html += "<td>"+terminal.fmodel+'</td>';
			html += "<td>"+terminal.fcompany+'</td></tr>';
		});
	}else{
		html = "<tr><td colspan='8' align='center'>暂无数据</td></tr>";
	}
	$("#terminal").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
		
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = new Array();
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	}); 
	return ids;  
}
/**
 * 字体切换
 * @param val
 */
function changeFont(val){
	if(val==1){
		//矢量
		$("#fontsizeTr").css("display","none");
	}else{
		//点阵字库
		$("#fontsizeTr").css("display","");
	}
}
/**
 * 添加网站页面
 */
function addTerminalHtml(data){
	var table = '<form action="addTerminal" method="post" name="terminalForm" id="terminalForm" onsubmit="formIsNull(this)">';
	table += '<div class="tab-page" id="baseinfo" style="display: block;">';
	table += '<table width="495" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
	table += '<tr><td height="25" width="70">设备名称</td><td height="25"><input class="text" name="fname" type="text" id="fresolutionx" size="30" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25" width="70">设备类型</td><td height="25"><select name="fdevicetype"  onchange="hidOrShow(this.value,\'hidd\')" style="width:100px;"><option value="1">机顶盒</option><option value="0">电脑</option><option value="2">手机</option><option value="3">Pad</option></select></td></tr>';
	table += '<tbody id="hidd"><tr><td height="25">解析度</td><td height="25"><select name="ftype" style="width:100px;"><option value="HD">高清</option><option value="SD">标清</option></select></td></tr>';
	table += '<tr><td height="25">字库</td><td height="25"><select name="ffont" onchange="changeFont(this.value);" style="width:100px;"><option value="1">矢量</option><option value="0">点阵</option></select></td></tr>';
	table += '<tr style="display:none;" id="fontsizeTr"><td height="25">字体大小</td><td height="25"><input name="ffontsize" type="text" id="ffontsize" size="30" onkeyup="value=this.value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;" value=""/></td></tr>';
	table += '<tr><td height="25">水平分辨率</td><td height="25"><input name="fresolutionx" id="fresolutionx" size="30" onkeyup="value=this.value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;" value=""/></td></tr>';
	table += '<tr><td height="25">垂直分辨率</td><td height="25"><input name="fresolutiony" id="fresolutiony" size="30" onkeyup="value=value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;" value="" /></td></tr></tbody>';
	table += '<tr><td height="25">设备型号</td><td><input name="fmodel" type="text" id="fmodel" value="" size="40" style="border: 0;border-bottom: 1px solid #ABADB3;"/></tr>';
	table += '<tr><td height="25">生产厂家</td><td height="25"><input name="fcompany" type="text" id="fcompany" size="40" value="&nbsp;" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">备注</td><td height="25"><textarea name="fremarks" id="fremarks" style="width:350px;height:45px;resize:none;"/></textarea></td></tr>';
	table += '</table></div></form>';

	var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'templateHtmlcloseBtn\').click();"/>&nbsp;&nbsp;&nbsp;<input type="image" alt="Submit" src="/CMS/images/box/submit_button.jpg" class="btn" id="submit" onclick="submitTerminal(\'addTerminal\')" />';
	Box("templateHtml",520,370,table,footerhtml,910);
}

/**
 * 提交保存网站数据
 */
function submitTerminal(uri){
	if(formIsNull($("#terminalForm")))
		$("#terminalForm").ajaxSubmit({
			url:uri,
			type:'post',
			dataType:'json',
			success:function(data){
				$.growlUI('数据保存成功');
				if(uri=="addTerminal")
					$("#terminalForm")[0].reset();
				else{
					$("#cancel").click();
				}
//				getSource();
				$('a[title=1]').click();
			},
			error:function(msg){
				$.growlUI('数据保存失败'); 
			}
		});
}

/**
 * 添加网站弹出信息
 */
function addTerminal(){
	addTerminalHtml();
	/*$.ajax({
		url:"addTerminalIndex",
		type:"post",
		dataType:"json",
		success:function(data){
			addTerminalHtml(data);
		},
		error:function(data){
			$.growlUI("数据加载出错"); 
		}
	});*/
}
/**
 * 加载数据
 * @param id
 */
function editAjax(id){
	var data = {"id":id};
	$.ajax({
		url:"queryTerminalById",
		data:data,
		type:"post",
		dataType:"json",
		success:function(data){
			bindGridEdit(data);
		},
		error:function(data){
			$.growlUI('数据加载出错'); 
		}
	
	});
}
/**
 * 编辑加载数据
 * @param id
 */
function editTerminal(id){
	var ids = checkedInfo();
	if(ids.length == 1){
		editAjax(ids[0]);
	}else if(ids.length == 0){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要修改的信息</div>";	
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}else if(ids.length > 1){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;只能修改1条信息！</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 绑定编辑数据
 * @param data
 */
function bindGridEdit(data){
//	var website = data.website;
	var terminal = data.terminal;
	var checked = "selected='selected'";
	var table = '<form action="updateTerminal" method="post" name="terminalForm" id="terminalForm">';
	table += '<div class="tab-page" id="baseinfo" style="display: block;">';
	table += '<table width="495" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
	table += '<tr><td height="25" width="70">设备名称</td><td height="25"><input class="text" name="fname" type="text" id="fresolutionx" size="30" value="'+(terminal.fname == undefined ? "" : terminal.fname)+'" style="border: 0;border-bottom: 1px solid #ABADB3;"/><input name="id" type="hidden" id="id" value="'+terminal.id+'"/></td></tr>';
	table += '<tr><td height="25" width="70">设备类型</td><td height="25"><select name="fdevicetype"  onchange="hidOrShow(this.value,\'hidd\')" style="width:100px;"><option value="0" '+(terminal.fdevicetype == 0 ? checked : "")+'>电脑</option><option value="1" '+(terminal.fdevicetype == 1 ? checked : "")+'>机顶盒</option><option value="2" '+(terminal.fdevicetype == 2 ? checked : "")+'>手机</option><option value="3" '+(terminal.fdevicetype == 3 ? checked : "")+'>Pad</option></select></td></tr>';
	table += '<tbody id="hidd" style="display:'+(terminal.fdevicetype==1?'':'none')+'"><tr><td height="25">解析度</td><td height="25"><select name="ftype" style="width:100px;"><option value="HD" '+(terminal.ftype == "HD" ? checked : "")+'>高清</option><option value="SD" '+(terminal.ftype == "SD" ? checked : "")+'>标清</option></select></td></tr></td></tr>';
	table += '<tr><td height="25">字库</td><td height="25"><select name="ffont" onchange="changeFont(this.value);" style="width:100px;"><option value="1" '+(terminal.ffont == 1 ? checked : "")+'>矢量</option><option value="0" '+(terminal.ffont == 0 ? checked : "")+'>点阵</option></select></td></tr>';
	table += '<tr style="display:'+(terminal.ffont == 1?'none':'')+';" id="fontsizeTr"><td height="25">字体大小</td><td height="25"><input class="text" name="ffontsize" type="text" id="ffontsize" size="30" onkeyup="value=this.value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;" value="'+terminal.ffontsize+' "/></td></tr>';
	table += '<tr><td height="25">水平分辨率</td><td height="25"><input name="fresolutionx" type="text" id="fresolutionx" value="'+terminal.fresolutionx+'" size="30" onkeyup="value=this.value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">垂直分辨率</td><td height="25"><input name="fresolutiony" type="text" id="fresolutiony" size="30" value="'+terminal.fresolutiony+'" onkeyup="value=value.replace(/[^\\d]/g,\'\')" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr></tbody>';
	/*table += '<tr><td height="25">网站名称</td><td height="25"><select name="fwebsiteinfoid" style="width:100px;">';
	$.each(website,function(i,web){
		table += '<option value="'+web.id+'" '+(web.id==terminal.fwebsiteinfoid?checked:"")+'>'+web.ftitle+'</option>';
	});
	table += '</select></td></tr>';*/
	table += '<tr><td>设备型号</td><td><input name="fmodel" type="text" id="fmodel" value="'+terminal.fmodel+'" size="40" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">生产厂家</td><td height="25"><input name="fcompany" type="text" id="fcompany" size="40" value="'+terminal.fcompany+'&nbsp;" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
	table += '<tr><td height="25">备注</td><td height="25"><textarea name="fremarks" id="fremarks" style="width:350px;height:45px;resize:none;"/>'+terminal.fremarks+'</textarea></td></tr>';
	table += '</table></div></form>';
    
	var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'terminalHtmlcloseBtn\').click();"/>&nbsp;&nbsp;&nbsp;<input type="image" alt="Submit" src="/CMS/images/box/submit_button.jpg" class="btn" id="submit" onclick="submitTerminal(\'updateTerminal\')" />';
	Box("terminalHtml",520,370,table,footerhtml,910);
}
/**
 * 删除站点信息
 */
function deleteTerminal(){
	var ids = checkedInfo();
	if(ids.length > 0){
		if(confirm("确定要删除设备信息吗？删除后无法恢复！")){
			var s = "";
		    for(var i=0;i<ids.length ;i++){
		        s +=ids[i]+",";
		    }
		    if(s != ""){
		    	s = s.substring(0,s.length-1) ;
		    }
			var data = {"ids":s};
			$.ajax({
				url:"deleteTerminal",
				data:data,
				type:"post",
				success:function(data){
					$.growlUI('成功删除数据'+data+'条');
					//location.reload();
//					getSource();
					$('a[title=1]').click();
				},
				error:function(data){
					$.growlUI('数据删除失败'); 
				}
			
			});
		}
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要删除的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 为stb时隐藏
 * @param obj
 */
function hidOrShow(val,hid){
	hid = hid == undefined ? "hid" : hid;
	if(val==1){
		$("#"+hid).css("display","");
	}else{
		$("#"+hid).css("display","none");
	}
}